package com.atguigu.java.ai.langchain4j.store;

import com.atguigu.java.ai.langchain4j.bean.ChatMessages;
import dev.langchain4j.data.message.ChatMessage;
import dev.langchain4j.data.message.ChatMessageDeserializer;
import dev.langchain4j.data.message.ChatMessageSerializer;
import dev.langchain4j.store.memory.chat.ChatMemoryStore;
import jakarta.annotation.Resource;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @Author: Mikey
 * @Date: 2025/8/30 12:31
 * @Description:
 **/
@Component
public class MongoChatMemoryStore implements ChatMemoryStore {

    @Resource
    private MongoTemplate mongoTemplate;

    @Override
    public List<ChatMessage> getMessages(Object memoryId) {
        //返回内容集合
        //根据memoryId创建一个查询
        Query query = new Query(Criteria.where("memoryId").is(memoryId));
        ChatMessages chatMessages = mongoTemplate.findOne(query, ChatMessages.class);
        //判断是否为空 若为空返回空集合  不处理就会报空指针异常
        if(chatMessages== null){
            return Collections.emptyList();
        }
        //使用聊天消息反序列化器将内容字段转化为一个集合返回  ChatMessageSerializer是聊天消息序列化器
        return ChatMessageDeserializer.messagesFromJson(chatMessages.getContent());


        //获取该文档将内容转化为数组返回
//        Criteria criteria = Criteria.where("memoryId").is(memoryId);
//        Query query = new Query(criteria);
//        ChatMessages chatMessages = mongoTemplate.findOne(query, ChatMessages.class);
//        if(chatMessages==null){
//            return Collections.emptyList();
//        }
//        return ChatMessageDeserializer.messagesFromJson(chatMessages.getContent());
    }

    @Override
    public void updateMessages(Object memoryId, List<ChatMessage> messages) {
        //构建一个查询对象
        Query query = new Query(Criteria.where("memoryId").is(memoryId));
        //构建一个修改对象
        Update update = new Update();
        //这时需要用聊天消息序列化器将集合进行序列化
        update.set("content",ChatMessageSerializer.messagesToJson(messages));
        //使用upsert进行修改  若不存在就新增
        mongoTemplate.upsert(query,update,ChatMessages.class);


//        Query query = new Query(Criteria.where("memoryId").is(memoryId));
//        Update update = new Update();
//        update.set("content", ChatMessageSerializer.messagesToJson(messages));
//
//        mongoTemplate.upsert(query,update,ChatMessages.class);
    }

    @Override
    public void deleteMessages(Object memoryId) {
        //构建一个查询对象
        Query query = new Query(Criteria.where("memoryId").is(memoryId));

        //进行删除
        mongoTemplate.remove(query,ChatMessages.class);


//        Query query = new Query(Criteria.where("memoryId").is(memoryId));
//        mongoTemplate.remove(query,ChatMessages.class);
    }
}
